package org.batatasframework.chart.graphics;

import java.util.ArrayList;
import java.util.List;

import org.batatasframework.chart.columns.ChartColumnAxis;
import org.batatasframework.chart.columns.ChartColumnAxisIterable;
import org.batatasframework.chart.columns.ChartColumnValue;

/**
 * <pre>一个界面图形的定义，可以定义的图形类型参考：{@link ChartGraphicType}。
 * 可以对图形的顺序进行指定，默认为rodinal=100，如果小于100的值，会被排在前面</pre>
 * 
 * <pre>生成的统计图形在界面上被分为两种，小图形和大图形；
 * 小图形：一个界面会显示两个统计图形，但如果统计的图形是线性或直方图的，如果被统计
 * 的axis比较多(超过{@link #defaultColumn}定义的个数的)，则会被限制在{@link #defaultColumn}定义
 * 个数之内，这时可以展开查看大图
 * 大图形：线性图、直方图等超过了{@link #defaultColumn}定义个数，都可以以大图形展示；
 * 如果是地图的形式的，则只能以大图形展示，无法被设置为小图形</pre>
 * @author jackson
 *
 * @see ChartGraphicAxis
 * @see ChartGraphicType
 */
public class ChartGraphic implements Comparable<ChartGraphic>{
	
	/** 统计图形的名字 */
	private final String name;
	
	/** 对那个字段，即轴(axis)进行图形统计 */
	private final ChartGraphicAxis axis;
	
	/** 图形的类型 */
	private final ChartGraphicType type;
	
	/** 需要被统计的字段，这些字段的结果值必须都为数值形式，如果非数值，则无法进行统计 */
	private final List<ChartColumnValue> lines = new ArrayList<>();
	
	private int defaultColumn = 5;
	private boolean maxable;
	private int ordinal = 100;
	
	public ChartGraphic(String name, ChartGraphicType type, ChartGraphicAxis axis){
		this.name = name;
		this.type = type;
		this.axis = axis;
	}
	
	@Override
	public int compareTo(ChartGraphic o) {
		return ordinal - o.ordinal;
	}
	
	public ChartGraphic ordinal(int ordinal){
		this.ordinal = ordinal;
		return this;
	}

	public void add(ChartColumnValue column) {
		lines.add(column);
	}
	
	@Override
	public String toString() {
		return name;
	}

	public String resolve(ChartColumnAxis axis) {
		StringBuilder html = new StringBuilder();
		html.append("{name:'").append(name).append("'");
		html.append(",maxable:").append(maxable);

		html.append(",axis:[");
		axisResolve(html, this.axis.resolve(axis));
		html.append("]");
		
		internalResolve(html, axis);
		html.append('}');
		return html.toString();
	}
	
	protected void axisResolve(StringBuilder html, ChartColumnAxis axis){
		if(axis instanceof ChartColumnAxisIterable){
			boolean s = false;
			for(ChartColumnAxis item : (ChartColumnAxisIterable) axis){
				if(s) html.append(',');
				else s = true;
				axisResolve(html, item);
			}
		}else{
			__axisResolve(html, axis);
		}
	}
	
	protected void __axisResolve(StringBuilder html, ChartColumnAxis axis){
		html.append("{ordinal:'").append(axis.getOrdinal()).append("'");
		html.append(",name:'").append(axis).append("'");
		html.append("}");
	}
	
	protected void internalResolve(StringBuilder html, ChartColumnAxis axis){
		html.append(",limit:").append(defaultColumn);
		html.append(",type:'").append(type.name().toLowerCase()).append("'");
		
		String ordinals = "", legends = "";
		for(ChartColumnValue line : lines){
			ordinals += "," + line.getOrdinal();
			legends  += ",'" + line + "'";
		}
		
		html.append(",ordinals:[").append(ordinals.substring(1)).append("]");
		html.append(",legend:[").append(legends.substring(1)).append("]");
	}
}
